<?php

namespace app\admin\controller\cms;

use app\admin\model\User;
use app\common\controller\Backend;
use think\exception\DbException;
use think\response\Json;

/**
 * 会员订单管理
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{

    /**
     * OrderModel模型对象
     * @var \app\admin\model\cms\OrderModel
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\cms\OrderModel;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();

        $filter = $this->request->get("filter", '');
        $filter = (array)json_decode($filter, true);



        $list = $this->model;


        //手机号查询
        if ($filter['user_mobile'] ?? null){
            $user_ids = User::where('mobile','like',"%{$filter['user_mobile']}%")->column('id');
            if ($user_ids){
                $list = $list->whereIn('user_id',$user_ids);
            }
        }

        //订单号
        if ($filter['order_num'] ?? null){
            $list = $list->where('order_num','=',$filter['order_num']);
        }

        //订单应付金额
        if ($filter['price'] ?? null){
            $price = explode(',',$filter['price']);
            sort($price);
            $price_0 = $price[0] ? :0;
            $price_1 = $price[1] ? :0;
            $list = $list->where('price','>=',$price_0);
            $list = $list->where('price','<=',$price_1);
        }

        //用户实际支付金额
        if ($filter['cash_fee'] ?? null){
            $cash_fee = explode(',',$filter['cash_fee']);
            sort($cash_fee);
            $cash_fee_0 = $cash_fee[0] ? :0;
            $cash_fee_1 = $cash_fee[1] ? :0;
            $list = $list->where('cash_fee','>=',$cash_fee_0);
            $list = $list->where('cash_fee','<=',$cash_fee_1);
        }

        //订单支付状态 默认0:未支付,1:已支付
        if (isset($filter['pay_status']) ?? null) {
            $list = $list->where('pay_status', $filter['pay_status']);
        }

        //用户付款时间
        if ($filter['pay_time'] ?? null) {
            $pay_time = explode(' - ', $filter['pay_time']);
            $list = $list->whereBetween('pay_time', $pay_time);
        }

        //创建时间
        if ($filter['createtime'] ?? null) {
            $createtime = explode(' - ', $filter['createtime']);
            $list = $list->whereBetween('createtime', $createtime);
        }

        //更新时间
        if ($filter['updatetime'] ?? null) {
            $updatetime = explode(' - ', $filter['updatetime']);
            $list = $list->whereBetween('updatetime', $updatetime);
        }

        $list = $list->order($sort, $order)
            ->paginate($limit);
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

}
